<?php
namespace app\admin\controller;
use app\common\model\AdminUser;
use app\common\model\Store;

class Dashboard extends \C {

  public function index(){

    // halt(env("root_path")."public/data");
    // halt(disk_total_space(env("root_path")."public/data/"));

    $this->VD([
      "date"=>date("Y-m-d"),
      "userCount"=>AdminUser::where("status",1)->count(),
      "storeCount"=>Store::where("status",1)->count(),
      "diskUse"=>$this->format_bytes($this->get_dir_size(env("root_path")."public/data/")),
    ]);

    $t=<<<'CODE'
{layout name="/public/layout_index" /}

<div class="row">
    <div class="col-lg-4 col-md-4">
        <div class="card card-inverse card-primary">
            <div class="card-body">
                <div class="d-flex">
                    <div class="m-r-20 align-self-center">
                        <h1 class="text-white"><i class="ti-pie-chart"></i></h1></div>
                    <div>
                        <h3 class="card-title">用户数量</h3>
                        <h6 class="card-subtitle">微信小程序用户总数量</h6> </div>
                </div>
                <div class="row">
                    <div class="col-4 align-self-center">
                        <h2 class="font-light text-white">{$userCount} 个</h2>
                    </div>
                    <div class="col-8 p-t-10 p-b-20 align-self-center">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Column -->
    <!-- Column -->
    <div class="col-lg-4 col-md-4">
        <div class="card card-inverse card-success">
            <div class="card-body">
                <div class="d-flex">
                    <div class="m-r-20 align-self-center">
                        <h1 class="text-white"><i class="icon-cloud-download"></i></h1></div>
                    <div>
                        <h3 class="card-title">附件</h3>
                        <h6 class="card-subtitle">所有附件耗费的磁盘空间</h6> </div>
                </div>
                <div class="row">
                    <div class="col-4 align-self-center">
                        <h2 class="font-light text-white">{$diskUse}</h2>
                    </div>
                    <div class="col-8 p-t-10 p-b-20 text-right">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Column -->
    <!-- Column -->
    <div class="col-lg-4 col-md-4">
        <div class="card card-inverse card-danger">
            <div class="card-body">
                <div class="d-flex">
                    <div class="m-r-20 align-self-center">
                        <h1 class="text-white"><i class="icon-layers"></i></h1></div>
                    <div>
                        <h3 class="card-title">库位</h3>
                        <h6 class="card-subtitle">库位数量</h6> </div>
                </div>
                <div class="row">
                    <div class="col-4 align-self-center">
                        <h2 class="font-light text-white">{$storeCount} 个</h2>
                    </div>
                    <div class="col-8 p-t-10 p-b-20 text-right">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- Column -->
</div>




CODE;


    return $this->display($t,$this->VD());
  }


  public function format_bytes($size) {
    $units = array(' B', ' KB', ' MB', ' GB', ' TB');
    for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
    return round($size, 2).$units[$i];
  }
  function get_dir_size($dir_name){
    $dir_size =0;
     if (is_dir($dir_name)) {
         if ($dh = opendir($dir_name)) {
            while (($file = readdir($dh)) !== false) {
                  if($file !="." && $file != ".."){
                        if(is_file($dir_name."/".$file)){
                             $dir_size += filesize($dir_name."/".$file);
                       }
                       /* check for any new directory inside this directory */
                       if(is_dir($dir_name."/".$file)){
                          $dir_size +=  $this->get_dir_size($dir_name."/".$file);
                        }
                     }
               }
       }
    }
    closedir($dh);
    return $dir_size;
  }


}
